package com.example.user.service;


import com.example.user.common.Result;
import com.example.user.entity.ProductReview;
import com.example.user.mapper.ProductDetailInfoMapper;
import com.example.user.request.ProductDetailInfoRequest;
import com.example.user.response.ProductDetailInfoResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductDetailInfoService {


    @Autowired
    private ProductDetailInfoMapper productDetailInfoMapper;


    public ProductDetailInfoResponse getProductInfoDetail(Integer productId) {
        List<ProductDetailInfoResponse> productDetailInfoResponse = productDetailInfoMapper.getProductInfoDetail(productId);
        ProductDetailInfoResponse productDetailInfoResponse1 = productDetailInfoResponse.get(0);
        return productDetailInfoResponse1;

    }

    public Result addCart(ProductDetailInfoRequest productDetailInfoRequest) {

        int quantity = 0;
        quantity = quantity + 1;
        int count = productDetailInfoMapper.addCart(productDetailInfoRequest, quantity);
        if (count == 0) {
            return Result.error("400", "添加失败");
        }
        return Result.success("添加成功");
    }

    public Result collectShop(ProductDetailInfoRequest productDetailInfoRequest) {

        int productId = productDetailInfoRequest.getProductId();
        int count = productDetailInfoMapper.collectShop(productId);
        if (count == 0) {
            return Result.error("400", "收藏失败");
        }
        return Result.success("收藏成功");

    }

    public Result getReivews(int productId) {
        List<ProductReview> productReview = productDetailInfoMapper.getReivews(productId);
        return Result.success(productReview);
    }

    public Result addComment(ProductReview productReview) {

        int count = productDetailInfoMapper.addComment(productReview);
        if (count == 0) {
            return Result.error("400", "添加评论失败!");
        }
        return Result.success("添加评论成功!");
    }
}
